package cn.stylefeng.guns.modular.business.frequency.finance.controller;

import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.finance.service.FinanceService;
import cn.stylefeng.guns.modular.business.frequency.orderextrafee.entity.OrderExtraFee;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author jzy
 * @Date 2021/8/28 0028
 **/
@Api(tags = "额外费用controller")
@RestController
@RequestMapping("/api/extra-fee")
public class ExtraFeeController {

    @Resource
    private FinanceService financeService;

    /**
     *
     * @param orderExtraFee
     * @return
     */
    @ApiOperation(value = "额外费用新增")
    @PostMapping
    public ResponseData addOrderExtraFee(@RequestBody OrderExtraFee orderExtraFee) {
        StringBuilder errorMsg = new StringBuilder();
        if (!financeService.checkOrderExtraFeeAdd(orderExtraFee, errorMsg)) {
            return ResponseData.andaError(errorMsg.toString());
        }
        financeService.addOrderExtraFee(orderExtraFee);
        financeService.changeFinanceStatus(orderExtraFee.getOrderId());
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "额外费用编辑")
    @PutMapping
    public ResponseData updateOrderExtraFee(@RequestBody OrderExtraFee orderExtraFee) {
        StringBuilder errorMsg = new StringBuilder();
        if (!financeService.checkOrderExtraFeeUpdate(orderExtraFee, errorMsg)) {
            return ResponseData.andaError(errorMsg.toString());
        }
        financeService.updateOrderExtraFee(orderExtraFee);
        financeService.changeFinanceStatus(orderExtraFee.getOrderId());
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "额外费用详情")
    @GetMapping("/{id}")
    public ResponseData getExtraFeeDetail(@PathVariable String id) {
        OrderExtraFee extraFee = financeService.getExtraFeeDetail(id);
        return ResponseData.andaSuccess(extraFee);
    }

    @ApiOperation(value = "额外费用删除")
    @DeleteMapping("/{id}")
    public ResponseData deleteExtraFee(@PathVariable String id) {
        StringBuilder errorMsg = new StringBuilder();
        if (!financeService.checkOrderExtraFeeDelete(id, errorMsg)) {
            return ResponseData.andaError(errorMsg.toString());
        }
        financeService.deleteOrderExtraFee(id);
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "订单额外费用列表")
    @GetMapping("/{orderId}/list")
    public ResponseData getExtraFeeListByOrderId(@PathVariable String orderId,
                                             @RequestParam(value = "scene", required = false) Integer addScene) {
        List<OrderExtraFee> extraFeeList = financeService.getExtraFeeListByOrderId(orderId);
        if (extraFeeList != null && extraFeeList.size() > 0 && addScene != null) {
            extraFeeList = extraFeeList.stream().filter(e -> e.getAddScene().equals(addScene)).collect(Collectors.toList());
        }
        return ResponseData.andaSuccess(extraFeeList);
    }
}
